//
//  PersonHeadView.swift
//  OnceNectar
//
//  Created by 吴凌 on 2018/8/1.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
protocol PersonHeadViewDelegate: NSObjectProtocol {
    func followButtonTap(btn:UIButton)
    func editUserTap()
    func userATap()
    func userBTap()
}
class PersonHeadView: UIView,CircleView {
    weak var delegate: PersonHeadViewDelegate?
    private lazy var bgImg: UIImageView = {
        let view = UIImageView()
        view.frame = self.bounds
        view.contentMode = UIViewContentMode.scaleAspectFill
        view.clipsToBounds = true
        return view
    }()
    
    private lazy var otherView: UIView = {
        let view = UIImageView()
        let h:CGFloat = bgImg.mj_h / 5 * 2
        let y:CGFloat = bgImg.mj_h / 5 * 3
        view.frame = CGRect.init(x: 0, y: y, width: self.mj_w, height: h)
        view.backgroundColor = UIColor.black
        view.alpha = 0.7
        view.isHidden = true
        view.isUserInteractionEnabled = true
        return view
    }()
    private lazy var mineView: UIView = {
        let view = UIImageView()
        let h:CGFloat = bgImg.mj_h / 4 * 1
        let y:CGFloat = bgImg.mj_h / 4 * 3
        view.frame = CGRect.init(x: 0, y: y, width: self.mj_w, height: h)
        view.backgroundColor = UIColor.black
        view.alpha = 0.7
        view.isHidden = true
        view.isUserInteractionEnabled = true
        return view
    }()
    
    private lazy var otherUserImg: BaseCircleImageView = {
        let view = BaseCircleImageView()
        let h:CGFloat = otherView.mj_h - 20
        view.frame = CGRect.init(x: 12, y: 10, width: h, height: h)
        view.setCircle(rect: view.bounds, cornerRadius: view.height)
        view.isUserInteractionEnabled = true
        return view
    }()
    
    private lazy var otherUserName: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.white
        view.font = UIFont.systemFont(ofSize: 15)
        return view
    }()
    private lazy var followButton: UIButton = {
        let view = UIButton.init(type: UIButtonType.custom)
        view.titleLabel?.textColor = UIColor.white
        view.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        view.setTitle("+ 关注", for: UIControlState.normal)
        view.setTitle("取消关注", for: UIControlState.selected)
        view.layer.borderColor = UIColor.white.cgColor
        view.layer.borderWidth = 1.0
        view.isUserInteractionEnabled = true
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.followButtonClick(_:))))
        return view
    }()
    private lazy var otherLvLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.white
        view.font = UIFont.systemFont(ofSize: 13)
        view.text = "等级："
        return view
    }()
    private lazy var otherLv: UIButton = {
        let view = UIButton.init(type: UIButtonType.custom)
        view.titleLabel?.textColor = UIColor.white
        view.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        view.setTitle("LV.0", for: UIControlState.normal)
        view.layer.cornerRadius = view.height / 2
        view.layer.borderColor = UIColor.white.cgColor
        view.layer.borderWidth = 1.0
        return view
    }()
    private lazy var otherMatchLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.white
        view.font = UIFont.systemFont(ofSize: 13)
        view.text = "匹配度："
        return view
    }()
    private lazy var otherMatch: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.white
        view.font = UIFont.systemFont(ofSize: 13)
        return view
    }()
    private lazy var otherAttBtn: UIButton = {
        let view = UIButton.init(type: UIButtonType.custom)
        view.titleLabel?.textColor = UIColor.white
        view.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        view.setTitle("关注 0", for: UIControlState.normal)
        return view
    }()
    private lazy var otherLine: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.white
        view.font = UIFont.systemFont(ofSize: 15)
        view.text = "|"
        return view
    }()
    private lazy var otherFanBtn: UIButton = {
        let view = UIButton.init(type: UIButtonType.custom)
        view.titleLabel?.textColor = UIColor.white
        view.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        view.setTitle("粉丝 0", for: UIControlState.normal)
        return view
    }()
    
    private lazy var userImg: BaseCircleImageView = {
        let view = BaseCircleImageView()
        let y:CGFloat = mineView.mj_h/2
        view.frame = CGRect.init(x: 12, y: -y, width: mineView.mj_h, height: mineView.mj_h)
        view.setCircle(rect: view.bounds, cornerRadius: view.height)
        return view
    }()
    private lazy var userEditBtn: UIButton = {
        let view = UIButton.init(type: UIButtonType.custom)
        view.titleLabel?.textColor = UIColor.white
        view.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        view.setTitle("编辑资料", for: UIControlState.normal)
        view.isUserInteractionEnabled = true
        view.setImage(UIImage.init(named: "icon_redact"), for: UIControlState.normal)
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.editUserClick(_:))))
        return view
    }()
    private lazy var userName: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.white
        view.font = UIFont.systemFont(ofSize: 15)
        return view
    }()
    private lazy var userLv: UIButton = {
        let view = UIButton.init(type: UIButtonType.custom)
        view.titleLabel?.textColor = UIColor.white
        view.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        view.setTitle("LV.0", for: UIControlState.normal)
        return view
    }()
    private lazy var userLine: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.white
        view.font = UIFont.systemFont(ofSize: 15)
        view.text = "|"
        return view
    }()
    private lazy var userAttBtn: UIButton = {
        let view = UIButton.init(type: UIButtonType.custom)
        view.titleLabel?.textColor = UIColor.white
        view.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        view.setTitle("关注 0", for: UIControlState.normal)
        view.isUserInteractionEnabled = true
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.userAClick(_:))))
        return view
    }()
    private lazy var userFanBtn: UIButton = {
        let view = UIButton.init(type: UIButtonType.custom)
        view.titleLabel?.textColor = UIColor.white
        view.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        view.setTitle("粉丝 0", for: UIControlState.normal)
        view.isUserInteractionEnabled = true
        view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.userBClick(_:))))
        return view
    }()
    private lazy var userGenderBtn: UIButton = {
        let view = UIButton.init(type: UIButtonType.custom)
        view.setImage(UIImage.init(named: "left_male"), for: UIControlState.normal)
        view.setImage(UIImage.init(named: "left_female"), for: UIControlState.selected)
        return view
    }()
    private lazy var oldLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.white
        view.font = UIFont.systemFont(ofSize: 15)
        return view
    }()
    
    func userBClick(btn:UIButton) -> Void {
        self.delegate?.userBTap()
    }
    @objc private func followButtonClick(_ gesture: UITapGestureRecognizer)  {
        self.delegate?.followButtonTap(btn: followButton)
    }
    @objc private func editUserClick(_ gesture: UITapGestureRecognizer)  {
        self.delegate?.editUserTap()
    }
    @objc private func userAClick(_ gesture: UITapGestureRecognizer)  {
        self.delegate?.userATap()
    }
    @objc private func userBClick(_ gesture: UITapGestureRecognizer)  {
        self.delegate?.userBTap()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(bgImg)
        bgImg.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
        addSubview(otherView)
        addSubview(mineView)
        
        mineView.addSubview(userImg)
        mineView.addSubview(userEditBtn)
        userEditBtn.snp.makeConstraints { (make) in
            make.top.equalTo(5)
            make.right.equalTo(-10)
            make.width.equalTo(72)
        }
        mineView.addSubview(userName)
        userName.snp.makeConstraints { (make) in
            make.centerY.equalTo(userEditBtn.snp.centerY).offset(0)
            make.left.equalTo(userImg.snp.right).offset(15)
        }
        
        mineView.addSubview(userGenderBtn)
        userGenderBtn.snp.makeConstraints { (make) in
            make.top.equalTo(userEditBtn.snp.bottom).offset(5)
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(16)
        }
        mineView.addSubview(oldLabel)
        oldLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(userGenderBtn.snp.centerY)
            make.left.equalTo(userGenderBtn.snp.right).offset(15)
            make.width.equalTo(40)
        }
        mineView.addSubview(userLv)
        userLv.snp.makeConstraints { (make) in
            make.centerY.equalTo(userGenderBtn.snp.centerY)
            make.right.equalTo(userGenderBtn.snp.left).offset(-15)
            make.width.equalTo(30)
        }
        
        mineView.addSubview(userLine)
        userLine.snp.makeConstraints { (make) in
            make.bottom.equalTo(-8)
            make.centerX.equalTo(mineView.snp.centerX).offset(0)
        }
        mineView.addSubview(userAttBtn)
        userAttBtn.snp.makeConstraints { (make) in
            make.right.equalTo(userLine.snp.left).offset(-20)
            make.centerY.equalTo(userLine.snp.centerY).offset(0)
        }
        mineView.addSubview(userFanBtn)
        userFanBtn.snp.makeConstraints { (make) in
            make.left.equalTo(userLine.snp.right).offset(20)
            make.centerY.equalTo(userLine.snp.centerY).offset(0)
        }
        
        otherView.addSubview(otherUserImg)
        otherView.addSubview(otherUserName)
        otherUserName.snp.makeConstraints { (make) in
            make.left.equalTo(otherUserImg.snp.right).offset(15)
            make.top.equalTo(otherUserImg.snp.top).offset(0)
        }
        otherView.addSubview(followButton)
        followButton.snp.makeConstraints { (make) in
            make.left.equalTo(otherUserName.snp.right).offset(15)
            make.centerY.equalTo(otherUserName.snp.centerY).offset(0)
        }
        otherView.addSubview(otherLvLabel)
        otherLvLabel.snp.makeConstraints { (make) in
            make.left.equalTo(otherUserImg.snp.right).offset(15)
            make.centerY.equalTo(otherView.snp.centerY).offset(0)
        }
        otherView.addSubview(otherLv)
        otherLv.snp.makeConstraints { (make) in
            make.left.equalTo(otherLvLabel.snp.right).offset(5)
            make.centerY.equalTo(otherView.snp.centerY).offset(0)
        }
        otherView.addSubview(otherMatchLabel)
        otherMatchLabel.snp.makeConstraints { (make) in
            make.left.equalTo(otherLv.snp.right).offset(15)
            make.centerY.equalTo(otherView.snp.centerY).offset(0)
        }
        otherView.addSubview(otherMatch)
        otherMatch.snp.makeConstraints { (make) in
            make.left.equalTo(otherMatchLabel.snp.right).offset(5)
            make.centerY.equalTo(otherView.snp.centerY).offset(0)
        }
        otherView.addSubview(otherLine)
        otherLine.snp.makeConstraints { (make) in
            make.bottom.equalTo(otherUserImg.snp.bottom).offset(0)
            make.centerX.equalTo(otherView.snp.centerX).offset(0)
        }
        otherView.addSubview(otherAttBtn)
        otherAttBtn.snp.makeConstraints { (make) in
            make.right.equalTo(otherLine.snp.left).offset(-20)
            make.centerY.equalTo(otherLine.snp.centerY).offset(0)
        }
        otherView.addSubview(otherFanBtn)
        otherFanBtn.snp.makeConstraints { (make) in
            make.left.equalTo(otherLine.snp.right).offset(20)
            make.centerY.equalTo(otherLine.snp.centerY).offset(0)
        }
        
        followButton.layer.cornerRadius = 14
        otherLv.layer.cornerRadius = 14
        userLv.layer.cornerRadius = 14
    }
    
    func setData(model:commentUserModel, isMine:Bool) {
        bgImg.sd_setImage(with: appendUrl(suffix: (model.headIcon)!), completed: nil)
        if isMine {
            mineView.isHidden = false
            userName.text = model.nickname
            userImg.sd_setImage(with: appendUrl(suffix: (model.headIcon)!), completed: nil)
            userLv.setTitle(model.grade?.gradeCode, for: UIControlState.normal)
            userGenderBtn.isSelected = model.sexy == "男" ? false : true
            let age :String = (model.age)!
            oldLabel.text = "\(age)岁"
            let cocernCount:String = (model.concernsAmount)!
            userAttBtn.setTitle("关注 \(cocernCount)", for: UIControlState.normal)
            let fanCount:String = (model.fansAmount)!
            userFanBtn.setTitle("粉丝 \(fanCount)", for: UIControlState.normal)
        }else {
            otherView.isHidden = false
            otherUserName.text = model.nickname
            otherUserImg.sd_setImage(with: appendUrl(suffix: (model.headIcon)!), completed: nil)
            otherLv.setTitle(model.grade?.gradeCode, for: UIControlState.normal)
            otherMatch.text = model.matchDegree == "null" ? "" : model.matchDegree
            let cocernCount:String = (model.concernsAmount)!
            otherAttBtn.setTitle("关注 \(cocernCount)", for: UIControlState.normal)
            let fanCount:String = (model.fansAmount)!
            otherFanBtn.setTitle("粉丝 \(fanCount)", for: UIControlState.normal)
            followButton.isSelected = Int(model.concernType!)! > 0 ? true : false
        }
    }
    
    required convenience init?(coder aDecoder: NSCoder) {
        self.init(coder: aDecoder)
    }
    
    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        for tempView in self.subviews {
            if tempView.isKind(of: UIView.self) {
                for v in tempView.subviews {
                    if v.isKind(of: UIButton.self) {
                        let button = v as! UIButton
                        let newPoint = self.convert(point, to: button)
                        if button.bounds.contains(newPoint) {
                            return true
                        }
                    }
                }
            }
        }
        return false
    }

}
